from django.db import models
import json
import requests
import time

# Create your models here.
# -*- coding: UTF-8 -*-

deviceNoDic = {
    "40403c995bd9ebdbe5324f196a477ad8": "1",
    "45e3ae0092a9399d48f4ec60d7981d0c": "2",
    "10cfc7e1faa273df928d4fba3baed1f4": "2",
    "5ab7ea41956fcf9d1589c9d04e778422": "1",
    "9512ca4b36deb70d18174d743f2db237": "2",
    "a02042ea30221577da5b6b3075102e9d": "1",
    "3cc3ca579723327efc269df2ecfbec06": "1",
    "02ee6f045f14cbf786fe4bf997b3e029": "1",
    "8617d11d2ab7c406773a7fb03c2478de": "2",
    "6f023fd79fddc3f4864be41fa4ef3461": "2",
    "a3cc9ffc8b5a4070f61720c102a48fad": "1",
    "2ee60799d13dd8f7e49e1b2d6d8a313f": "2",
    "70f47d34d80618ea3114e3cfa4c64625": "1"
}

session = requests.Session()

data = {"washTemperatureId": 1, "selfCleanId": 0, "type": 1, "deviceTypeId": 2}
freeDevices = []
authorization = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9' \
                '.eyJpZCI6NzkxNjAxNSwibmFtZSI6IjEzODU1MTQ5MTM2IiwiYXBwVXNlcklkIjo1MTA2NzU5LCJpYXQiOjE2MTYyMzE5ODIsImV4cCI6MTYyNDI2NzE4Mn0.n6i_FerSrZVLZ585MkN36-Cy2VYSmJvVVzvzV4RefzE '

heasers = {'x-mobile-brand': 'meizu',
           'x-mobile-id': 'c18a9b84-dd9a-3457-bc66-a79770f3fcb2',
           'x-user-geo': '117.257308,31.864423',
           'x-app-version': '2.1.8',
           'x-mobile-model': '16s',
           'accept-encoding': 'gzip',
           'user-agent': 'okhttp/4.3.1',
           'Host': 'phoenix.ujing.online'}
orderHeaders = {'Host': 'phoenix.ujing.online',
                'x-mobile-brand': 'Xiaomi',
                'x-app-code': 'BA',
                'user-agent': 'Mi MIX 2S(Android/9) (com.midea.vm.washer/2.1.8) Weex/0.28.0 1080x2160',
                'x-app-version': '2.1.8',
                'x-mobile-model': 'Mi MIX 2S',
                'content-type': 'application/json; charset=utf-8',
                'content-length': '170',
                'accept-encoding': 'gzip'}



def getDevices():
    global session
    global heasers
    global freeDevices
    global deviceNoDic
    freeDevices = []
    heasers['authorization'] = authorization
    session.headers.update(heasers)
    for count in range(10):
        s = session.get(
            "https://phoenix.ujing.online/api/v1/stores/near?page={}&size=10&lat=31.864423&lont=117.257308&scope=2000&mode=BA".format(
                count + 1))
        time.sleep(0.1)
        result = s.json()
        for i in result['data']['storeList']:
            if i['name'].__contains__("12B"):
                for device in i['devices']:
                    if device['status'] == '0':
                        try:
                            count = deviceNoDic[device['virtualId']]
                            # print("111")
                            freeDevices.append(
                                {'deviceId': device['virtualId'], 'storeId': device['storeId'],
                                 'name': i['name'] + " " + str(count) + "号滚筒"})
                        except:
                            count = getDeviceNo(device['virtualId'])
                            # print("222")
                            freeDevices.append(
                                {'deviceId': device['virtualId'], 'storeId': device['storeId'],
                                 'name': i['name'] + " " + str(count) + "号滚筒"})
    freeDevices = sorted([dict(t) for t in set([tuple(d.items()) for d in freeDevices])], key=lambda x: x['name'])
    # print(deviceNoDic)
    return freeDevices


def order(deviceId, storeId):
    global session
    global orderHeaders
    global data
    orderHeaders['authorization'] = authorization
    data['deviceId'] = deviceId
    data['storeId'] = storeId
    data['deviceWashModelId'] = 2
    return requests.post("https://phoenix.ujing.online/api/v1/orders/create", data=json.dumps(data),
                         headers=orderHeaders).text


def getDeviceNo(deviceid):
    global session
    return session.get(f"https://phoenix.ujing.online/api/v1/devices/{deviceid}").json()['data']['device']['no']


if __name__ == '__main__':
    result = getDevices()
    print(result)
